package xuwei_k

import javax.servlet.http._

class Main extends HttpServlet {
  
  override def doGet( req:HttpServletRequest,  resp:HttpServletResponse){
			
    val x = xml.XML.load(new java.net.URL("http://github.com/api/v2/xml/commits/list/scala/scala/master"))
		
    val commits = ( x \\ "commits" \ "commit" ).map{Commit.apply}
    
    val oldCommits = DB.selectAll
    val newCommits = commits.filterNot{c => oldCommits.contains(c.id)}.toSeq
    
    DB.insert(newCommits.map{_.id}:_*)
    
    newCommits.take(5).foreach{t => TweetClient.tweet(t.tweetString)}
    
    resp.getWriter.println(newCommits.map{_.tweetString})
  }
}
/*
object Googl{
  import scalaj.http._
  import scala.util.parsing.json.JSON
  
  def getShortUrl(longUrl:String):String = {
    val data = 
      Http.postData("https://www.googleapis.com/urlshortener/v1/url",
      """{ "longUrl" :  "%s" }""".format(longUrl)    
      ).header("Content-Type","application/json").asString

    JSON.parseFull(data).get.asInstanceOf[scala.collection.Map[String,String]].apply("id")
  }
}
*/
